#include<iostream>
#include<algorithm>

using namespace std;

typedef long long ll;
const int N = 5e4 + 10;

struct node
{
	ll weight;
	ll strong;

}a[N];

bool cmp(node& x1, node& x2)
{
	return max(x2.weight - x1.strong, -x2.strong) < max(x1.weight - x2.strong, -x1.strong);
}

int main()
{
	int n; cin >> n;
	ll total_weight = 0; ll ret= -0x3f3f3f3f3f3f3f3f;
	for (int i = 1; i <= n; i++)  cin >> a[i].weight,
		cin >> a[i].strong, total_weight += a[i].weight;

	sort(a + 1, a + 1 + n, cmp);

	for (int i = 1; i <= n; i++)
	{
		total_weight -= a[i].weight;
		ret = max(ret, total_weight - a[i].strong);
	}

	cout << ret;

	return 0;
}
